package com.hcb.tonguetiphealth.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader;
import com.hcb.tonguetiphealth.manager.AsyncBitmapLoader.ImageCallBack;
import com.hcb.tonguetiphealth.manager.ChatManager;
import com.hcb.tonguetiphealth.manager.SmileyParser;
import com.hcb.tonguetiphealth.model.Chat;
import com.hcb.tonguetiphealth.model.User;
import com.hcb.tonguetiphealth.view.R;

public class ChatListAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<Chat> msgs;
	private Context context;
	private SimpleDateFormat formatter;
	private ChatManager chatManager;

	public ChatListAdapter(Context context) {
		this.context = context;
		mInflater = LayoutInflater.from(this.context);
		msgs = new ArrayList<Chat>();
		formatter = new SimpleDateFormat("MM-dd HH:mm:ss");
		chatManager = new ChatManager((Activity)context);
	}
	
	public List<Chat> getMsgs() {
		return msgs;
	}
	public void setMsgs(List<Chat> msgs) {
		this.msgs.clear();
		this.msgs.addAll(msgs);
		notifyDataSetChanged();
		//this.msgs = msgs;
	}
	
	public void addMessage(Chat msg){
		msgs.add(msg);
		chatManager.saveChat(msg);
		notifyDataSetChanged();
	}
	
	public void addMessages(List<Chat> msgList){
		msgs.addAll(msgList);
		notifyDataSetChanged();
	}
	
	public void clear(){
		msgs.clear();
		notifyDataSetChanged();
	}
	
	public void addUser(User user){
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return msgs.size();
	}

	@Override
	public Chat getItem(int position) {
		return msgs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		/*final ViewHolder holder;
		Chat msgInfo = getItem(position);
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.homepage_personal_chat_list_item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.setData(msgInfo);*/
		
		Chat msgInfo = getItem(position);
		boolean mySelf;
		//if(convertView == null) {
			if(msgInfo.getUserName().equals(Global.user.getUserName())) {
				mySelf = true;
				convertView = mInflater.inflate(R.layout.homepage_personal_chat_list_item_1, null);
			} else {
				mySelf = false;
				convertView = mInflater.inflate(R.layout.homepage_personal_chat_list_item_2, null);
			}
		//}
		FillDataIntoView(convertView, position, mySelf);
		return convertView;
	}
	private void FillDataIntoView(View view, final int position, boolean mySelf) {
		Chat msgInfo = getItem(position);
		ImageView image = (ImageView) view.findViewById(R.id.team_singlechat_id_listiteam_headicon);
		final TextView text = (TextView) view.findViewById(R.id.team_singlechat_id_listiteam_message);
		if(mySelf) {
			TextView tv_time = (TextView)view.findViewById(R.id.team_singlechat_id_time);
			tv_time.setText(formatter.format(new Date(msgInfo.getTime()*1000)));
		}
		Bitmap bitmap = AsyncBitmapLoader.getInstance().loadBitmap(image, msgInfo.getPotraitURL(), new ImageCallBack() {  
            public void imageLoad(ImageView imageView, Bitmap bitmap) { 
            	if(bitmap == null) {   
            		imageView.setImageResource(R.drawable.ic_launcher);     
        		} else {    
        			imageView.setImageBitmap(bitmap);    
        		}
            }  
        });  
		if(bitmap == null) {   
			image.setImageResource(R.drawable.ic_launcher);     
		} else {    
			image.setImageBitmap(bitmap);    
		}		//设置头像
		String str = msgInfo.getMsg().trim();	  //消息具体内容
		if(str.toLowerCase().startsWith("http://") && (str.endsWith(".jpg")
												    || str.endsWith(".png")
										            || str.endsWith(".gif"))) {
			System.out.println("-------------聊天记录图片下载地址 ： "  + str);
			SmileyParser.getInstance().downPicture(str, text);
		} else if(str.contains("/mnt/") || str.contains("/data/data/")) {
			SmileyParser.getInstance().decodeFile(str, text);
		} else {
			if(!mySelf) {
				str = str.replaceAll("<br />", "\n");
			}
			try {
				CharSequence convertContent = SmileyParser.getInstance()
				.addSmileySpans(str);
				text.setText(convertContent);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			}
		}
	}
	@Override
	public boolean isEnabled(int position) {
		// TODO Auto-generated method stub
		//return super.isEnabled(position);
		return false;
	}
	
}
